<template>
  <view
    ref="article-item"
  >
      <view v-if="articles" class="Panel-item">
        <view class="item" v-for="item in  articles" :key="item.id">
        <view class="avatar" v-if="showAvatar" @tap="goToUser(item.user_id)">
					<u-avatar :src="item.user.avatar " size="36"></u-avatar>
          <view class="author">
            <a>{{ item.user.nickname }}</a>
            <span class="introduce" >已关注 · {{item.user.introduce}}</span>
          </view>
        </view>
				
        <view  class="top"  v-if="cover.type === 0 || cover.type === 2 || cover.type === 3" @tap="goToArticle(item.id)" >
          <h1>{{ item.title }}</h1>
        </view>
				
        <view class="centre" v-if="cover.type === 2" @tap="goToArticle(item.id)">
					<u--image :showLoading="true" :src="item.cover_img" mode="widthFix" width="100%"></u--image>
        </view>
				
        <view class="centre-3" v-if="cover.type === 3" @tap="goToArticle(item.id)">
					<view class="image">
						<u--image :showLoading="true" :src="item.cover_img" mode="widthFix" width="100%" height="100%"></u--image>
					</view>
					<view class="image">
						<u--image :showLoading="true" :src="item.cover_img" mode="widthFix" width="100%"></u--image>
					</view>
					<view class="image">
						<u--image :showLoading="true" :src="item.cover_img" mode="widthFix" width="100%"></u--image>
					</view>
        </view>
        <view>
          <view class="bottom-1" v-if="cover.type === 1">
            <view class="left">
              <h1 @tap="goToArticle(item.id)">{{ item.title }}</h1>
              <view class="left-bottom">
                <view>
                  <span class="author-name" v-if="!showAvatar">{{
                    item.user.nickname
                  }}</span>
                  <span class="comment">{{ item.comment_count }}评论</span>
                  <span class="read">{{ item.read_count }}观看</span>
                  <span class="time">{{ item.createdAt | relativeTime }}</span>
                </view>
                <view>
                  <i class="iconfont icon-cha" @tap="onClickCha"></i>
                </view>
              </view>
            </view>
            <view class="right" @tap="goToArticle(item.id)">
							<u--image :showLoading="true" :src="item.cover_img" mode="widthFix" width="100%" ></u--image>
            </view>
          </view>
					
          <view
            class="bottom"
            v-if="cover.type === 0 || cover.type === 2 || cover.type === 3"
          >
            <view>
              <span class="author-name" v-if="!showAvatar">{{
                item.user.nickname
              }}</span>
              <span class="comment">{{ item.comment_count }}评论</span>
              <span class="read">{{ item.read_count }}观看</span>
              <span class="time">{{ item.createdAt | relativeTime }}</span>
            </view>
            <view>
              <i class="iconfont icon-cha" @tap="onClickCha" >
							</i>
            </view>
          </view>
        </view>
        </view>
      </view>

		
		
		
  </view>
</template>

<script>
import bus from '@/utils/bus'
// import { getArticles } from '@/api/home'
import { getArticleList } from '@/api/article'
// import Popover from '@/components/My-Popover.vue'
import { debounce } from 'lodash'
export default {
  name: 'ArticleItem',
  // components: { Popover },
  props: {
    config: {
      zero: false,
      one: false,
      two: false,
      three: false,
      showComment: true
      // showAuthorName: {
      //   type: Boolean,
      //   default: () => true
      // },
      // showComment: {
      //   type: Boolean,
      //   default: true
      // },
			// ,
   //    showAvatar: {
   //      type: Boolean,
   //      default: true
   //    },
      // showRead: {
      //   type: Boolean,
      //   default: true
      // },
      // showTime: {
      //   type: Boolean,
      //   default: true
      // }
    },
    data: {
      type: [Object,Array],
      required: false // 暂时不必填
    }
  },
  data () {
    return {
      showAvatar: true,// 是否显示图片
      cover: {
        type: 1, // 封面类型 0 无图；1 右边1张图；2 一张大图；3 三张图片；
        images: [] // 图片数组
      },
      teim: '2022-11-04',
      isCha: false,
      loading: false,
      skeletonLoading: true,
      finished: false,
      timestamp: null,
      scrollTop: 0, // 列表滚动到顶部的距离
      articles: [],
      pageNum: 2,
      pageSize: 10,
      total: 0
    }
  },
  watch: {
    articles: function (newVal, oldVal) {
      if (newVal.length >= 0) {
        this.skeletonLoading = false
      }
    },
    data: function (newVal, oldVal) {
      this.articles = newVal.list
      this.skeletonLoading = false
      console.log(newVal);
      console.log(this.articles);
    }
  },
  mounted () {
    const articleItem = this.$refs['article-item']
    articleItem.onscroll = debounce(() => {
      this.scrollTop = articleItem.scrollTop
    }, 50)
  },
  activated () {
    // 把记录的到顶部距离重新设置回去
    this.$refs['article-item'].scrollTop = this.scrollTop
    // this.onLoad()
  },
  methods: {
    async onLoad () {
      // this.skeletonLoading = true
      const res = await getArticleList({
        pageNum: this.pageNum,
        pageSize: this.pageSize
      })

      // this.skeletonLoading = false
      this.pageNum = this.pageNum + 1
      this.total = this.total + res.total

      this.articles.push(...res.list)
      // const res = getArticles({
      // channel_id: this.channel.id, // 频道id
      //   timestamp: this.timestamp || Date.now(), // 时间戳，请求新的推荐数据传当前的时间戳，请求历史推荐传指定的时间戳,timestamp相当于页码，请求最新数据使用单前最新时间戳，下一页数据使用上一次返回的数据中的时间戳
      //   with_top: 1 // 是否包含置顶，进入页面第一次请求时要包含置顶文章，1包含，0不包含
      // })

      // setTimeout(() => {
      //   this.articles.push(...res)
      // }, 1000)
      // this.articles.push(...res)
    },
    goToUser (id) {
     uni.navigateTo({ url: '/pages/user/index?user_id='+id});
    },
    goToArticle (id) {
    	uni.navigateTo({ url: '/pages/article/index?article_id=' + id });
    },
    onClickCha () {
      console.log('点击了差')
      this.isCha = true
      bus.$emit('onClickCha')
    }
  }
}
</script>
<style lang="scss" scoped>
	.Panel-item{
		background-color: rgb(245, 243, 243);
	}
.article-home {
  position: fixed;
  left: 0;
  right: 0;
  top: 97.95px;
  bottom: 50px;
  overflow-y: auto;
  background-color: rgb(245, 243, 243);
}

.item {
  display: flex;
  flex-direction: column;
  padding: 15px 15px 10px 15px;
  box-sizing: border-box;
  // height:330px;
  margin-top: 10px;
  background-color: #fff;
  .avatar {
    display: flex;
    margin-bottom: 10px;
    .author {
      display: flex;
      flex-direction: column;
      align-items: flex-start;
      margin-left: 10px;
      a {
        font-size: 14px;
        font-weight: 500;
      }
      span {
        font-size: 10px;
        color: #ccc;
      }
      .introduce{
        max-width: 190px;
        overflow: hidden; //溢出内容隐藏
          text-overflow: ellipsis; //文本溢出部分用省略号表示
          display: -webkit-box; //特别显示模式
          -webkit-line-clamp: 1; //行数
          -webkit-box-orient: vertical; //盒子中内容竖直排列
      }
    }
  }
  .top {
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    text-align: left;
    h1 {
      font-size: 16px;
      line-height:23px;
      margin: 0;
      overflow: hidden; //溢出内容隐藏
        text-overflow: ellipsis; //文本溢出部分用省略号表示
        display: -webkit-box; //特别显示模式
        -webkit-line-clamp: 2; //行数
        -webkit-box-orient: vertical; //盒子中内容竖直排列
    }
  }
  .centre {
    width: 100%;
    height: 200px;
    border-radius: 4px;
        overflow: hidden;
    margin-top: 5px;
    // .van-image {
    //   width: 100%;
    //   height: 200px;
    // }
  }
  .centre-3 {
    display: flex;
    justify-content: space-between;
    margin-top: 10px;
    border-radius: 4px;
    overflow: hidden;
				
    .image {
      width: 114px;
      height: 90px;
    }
  }

  .bottom {
    display: flex;
    justify-content: space-between;
    .icon-cha {
      font-size: 12px;
      color: #ccc;
    }
    span {
      font-size: 12px;
      color: #ccc;
      margin-right: 10px;
    }
  }
  .bottom-1 {
    display: flex;
    box-sizing: border-box;
    height: 90px;
    .left {
      // position: relative;
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      text-align: left;
      width: 230px;
      margin-right: 14px;
      h1 {
        font-size: 16px;
        line-height:23px;
        margin: 0;
        overflow: hidden; //溢出内容隐藏
        text-overflow: ellipsis; //文本溢出部分用省略号表示
        display: -webkit-box; //特别显示模式
        -webkit-line-clamp: 2; //行数
        -webkit-box-orient: vertical; //盒子中内容竖直排列
      }
      .left-bottom {
        display: flex;
        justify-content: space-between;

        .icon-cha {
          font-size: 12px;
          color: #ccc;
        }
        span {
          font-size: 12px;
          color: #ccc;
          margin-right: 10px;
        }
      }
    }
    .right {
      width: 120px;
      // height: 99px;
      border-radius: 4px;
      overflow: hidden;
    }
  }
}
</style>
